use axum::{
    response::{Html, IntoResponse},
    Extension, Json,
};
use hyper::StatusCode;
use rbatis::{sql::PageRequest, Rbatis};

use crate::{
    name_validation::NameInput, validated::validated::ValidatedForm, AdobeCompensateLog,
    PlatformPage,
};

pub async fn handler_404() -> Html<&'static str> {
    Html("<h1>404 Not Found</h1>")
}

pub async fn handler(
    Extension(mut r): Extension<Rbatis>,
) -> Result<impl IntoResponse, (StatusCode, String)> {
    // let _data = AdobeCompensateLog::select_by_name(&mut r, "123").await;
    let page = PageRequest::default();
    let page_data = AdobeCompensateLog::select_page(&mut r, &page).await;
    match page_data {
        Ok(data) => {
            tracing::info!("featch data success  {data:?}");
            Ok(Json(PlatformPage::new(data)))
        }
        Err(e) => {
            tracing::info!("featch data faild  {}", e);
            Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))
        }
    }
}

pub async fn validated_handler(ValidatedForm(input): ValidatedForm<NameInput>) -> Html<String> {
    Html(format!("<h1>Hello, {}!</h1>", input.name))
}
